package com.codeczx.myretrofitdemo.retrofit;

import okhttp3.*;
import okhttp3.Request;

/**
 * Created by codeczx on 2018/3/29.
 */

public class RequestBuilder {

    private final String method;
    private final HttpUrl baseUrl;
    private final String relativeUrl;
    private final Request.Builder requestBuilder;
    private HttpUrl.Builder urlBuilder;

    public RequestBuilder(String httpMethod, HttpUrl baseUrl, String relativeUrl) {
        this.method = httpMethod;
        this.baseUrl = baseUrl;
        this.relativeUrl = relativeUrl;
        requestBuilder = new Request.Builder();
    }

    public Request build() {
        HttpUrl url;
        HttpUrl.Builder urlBuilder = this.urlBuilder;
        if(urlBuilder!=null){
            url = urlBuilder.build();
        }else{
            url = baseUrl.resolve(relativeUrl);
        }
        return requestBuilder.
                url(baseUrl)
                .method(method,null)
                .build();
    }

    public void addPathParam(String name, String value, boolean encoded) {
        relativeUrl.replace("{"+name+"}",value);
    }
}
